/*
 License
 
 Copyright © 2012 Kevin Kimmich
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights to 
 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 the Software, and to permit persons to whom the Software is furnished to do so, 
 subject to the following conditions:
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 @file
 C exception handling.
 */
#ifndef _exception_h
#define _exception_h

#ifdef __cplusplus
extern "C" {
#endif


#include "crcore.h"

#define EC(x) ((x)+0x80000000)

extern const result_code MEMORY_ERROR;
extern const result_code SOCKET_ERROR;
extern const result_code SOCKET_TIMEOUT;

/*!
 The exception environment for each thread.
 */
typedef struct exception_env_t {
	struct exception_env_t* previous_env;
	const char* try_func_name;
	const char* throw_func_name;
	const result_code* code;
	char extended_information_string[LOCAL_BUF_SIZE];
	list_t garbage_list;
	jmp_buf env;
} exception_env_t;

/*!
 A structure for keeping track of items to trash in case of an exception.
 */
typedef struct garbage_list_node
{
	list_node_t node;
	void* data;
	void(*cleanup_func)(struct garbage_list_node*);
} garbage_list_node;


#define INIT_EXCEPTION {.previous_env=NULL,.code=NULL,.try_func_name=NULL,.throw_func_name=NULL,.extended_information_string={0},.garbage_list=LIST_INITIALIZER}

/*!
 Call this for each thread.
 */
void init_exception_environment(void);

/*!
 Private function for the exception handler.
 */
void push_exception_environment(exception_env_t* env);

/*!
 Get the current exception environment for the thread.
 */
exception_env_t* get_exception_environment(void);

/*!
 pop the current exception environment.
 */
exception_env_t* pop_exception_environment(void);

/*!
 cleanup the entire exception environment.
 @param env the exception environment.
 @param do_cleanup cleanup the garbage nodes.
 */
void cleanup_exception_environment(exception_env_t* env, int do_cleanup);

/*!
 add garbage to cleanup to the exception environment.
 @param data The item that will be cleaned up.
 @param cleanup_func The function to call to cleanup the item.
 */
garbage_list_node* add_garbage_node(void* data, void(*cleanup_func)(struct garbage_list_node*));

/*!
 remove a garbage node from the garbage list.
 @param node The node to remove.
 */
void remove_garbage_node(garbage_list_node* node);

/*!
 Print the exception information.
 @param prefix, a string to prepent to the exception information.
 @param e, the exception envirenment
 */
void print_exception(const char* prefix,const exception_env_t* e);

//******************************************************* 
//
//  C++ like exception handling:
//    Usage:
//      TRY {
//         some implementation
//      }
//      CATCH(exception_env_t) {
//         some exception handling
//      }
//		FINALIZE
//
//  THROW: When you want to throw an exception.
//  BUBBLE: to continue an exception's progress up the stack
//*******************************************************

#define TRY {\
int TRY_BLOCK_res=0;\
exception_env_t *TRY_BLOCK_env = (exception_env_t*) malloc(sizeof(exception_env_t));\
memset(TRY_BLOCK_env,0,sizeof(exception_env_t));\
list_init(&TRY_BLOCK_env->garbage_list);\
TRY_BLOCK_env->try_func_name=__func__;\
push_exception_environment(TRY_BLOCK_env);\
TRY_BLOCK_res = setjmp(TRY_BLOCK_env->env);\
if (TRY_BLOCK_res==0){

#define CATCH(exception) {exception_env_t *env = pop_exception_environment(); cleanup_exception_environment(env,0); free(env);}}else{\
{exception_env_t* env = pop_exception_environment();\
cleanup_exception_environment(env,1);\
exception.throw_func_name=env->throw_func_name;\
exception.try_func_name=env->try_func_name;\
exception.code=env->code; strncpy(exception.extended_information_string,env->extended_information_string,LOCAL_BUF_SIZE);\
free(env);}

#define FINALIZE }}

#define BUBBLE(exception){\
exception_env_t *env = get_exception_environment();\
if(env){\
env->code=exception.code; strncpy(env->extended_information_string,exception.extended_information_string,LOCAL_BUF_SIZE);\
longjmp(env->env,-1);}}

#define THROW(code_ptr,format,args...) {\
exception_env_t *env = get_exception_environment();\
if(env){\
env->code=(&code_ptr);\
env->throw_func_name=__func__;\
if(format!=NULL) snprintf(env->extended_information_string,LOCAL_BUF_SIZE,format,##args); else strcpy(env->extended_information_string,"");\
longjmp(env->env,-1);}}

	
#ifdef __cplusplus
}
#endif

	
#endif //#ifndef _exception_h
